1702F - Equate Multisets - CodeForces Solution


constructive algorithms data structures greedy math number theory *1700

Please click on ads to support us..

Python Code:

 
from collections import defaultdict,Counter
import math
import bisect
from itertools import accumulate
from math import ceil, log
 
from sys import stdin, stdout
 
def read():
    return stdin.readline().rstrip()
 
 
 
 
total = int(read())
 
 
for _ in range(total):
    s = read()
    
    
    a =  ([int(p) for p in input().split()])
    b =  ([int(p) for p in input().split()])
    
    c= defaultdict(lambda:0)
    for i in a:
        while i%2==0:
            i = i//2
        c[i] +=1
        for z in b:
        if c[z] >0:
            c[z] -=1
        else:
            while z >0:
               z = z//2
               if c[z] >0:
                   c[z] -=1
                   break
        if sum(c.values())==0:
        print("YES")
    else:
        print("NO")

C++ Code:

#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define all(a)  (a).begin(),(a).end()
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define eb emplace_back
#define pii pair<int,int>
#define lowbit(a) ((a)&(-a))
#define inv(x) fpow((x),(mod-2))
#define sub(x,y) ((x-y)%mod+mod)%mod
#define pt(x,m) ((x)%(m)+(m))%(m)
#define fc first
#define sc second
#define debug(a) cout<<#a<<":"<<(a)<<endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

ll mod=1e9+7;
ll fpow(ll x,ll y) {
	ll tmp=x%mod,res=1;
	while(y) {
		if(y&1)res=res*tmp%mod;
		tmp=tmp*tmp%mod;
		y>>=1;
	}
	return res;
}


inline int read(){
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c<='9'&&c>='0'){
		x=(x<<1)+(x<<3)+(c^48);
		c=getchar();
	}
	return x*f;
}


const int N=2e5+10;

void solve() {
	int n;
	n=read();
	map<int,int>a;
	unordered_map<int,int>b;
	unordered_map<int,vector<int>>e;
	vector<int>las;
	for(int i=0; i<n; i++) {
		int x;
		x=read();
		while(x&&(!(x&1))) {
			x>>=1;
		}
		a[x]++;
	}
	for(int i=0; i<n; i++) {
		int x;
		x=read();
		while(x&&!(x&1)) {
			x>>=1;
		}
		b[x]++;
		if(x==0)continue;
		int tmp=x;
		while(tmp) {
			e[tmp].eb(x);
			tmp>>=1;
		}
		e[0].eb(x);
	}
	auto iter=a.rbegin();
	while(iter!=a.rend()) {
		auto [val,cnt]=*iter;
		int chk=a[val];
		auto &v=e[val];
		for(int &i:v) {
			if(b[i]) {
				b[i]--;
				chk--;
				if(chk==0)break;
			}
		}
		if(chk)return puts("NO"),void();
		iter++; 
	}
	puts("YES");
}





int main() {
	int t;
	t=read();
	while(t--)solve();
}

















































Comments

Submit
0 Comments
More Questions

361A - Levko and Table
5E - Bindian Signalizing
687A - NP-Hard Problem
1542C - Strange Function
961E - Tufurama
129D - String
888A - Local Extrema
722B - Verse Pattern
278A - Circle Line
940A - Points on the line
1742C - Stripes
1742F - Smaller
1742B - Increasing
1742A - Sum
1742D - Coprime
390A - Inna and Alarm Clock
1398B - Substring Removal Game
1742G - Orray
416B - Art Union
962A - Equator
803B - Distances to Zero
291A - Spyke Talks
1742E - Scuza
1506D - Epic Transformation
1354G - Find a Gift
1426F - Number of Subsequences
1146B - Hate "A"
1718C - Tonya and Burenka-179
834A - The Useless Toy
1407D - Discrete Centrifugal Jumps